package com.test.typeGame.core;

import com.test.typeGame.TypeGameApp;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import java.util.Vector;

public class TGPanel extends Panel implements Runnable, KeyListener,ActionListener {
    private int gameLevel = 0;
    private boolean startRun = true;
    Vector<String> gameLevelName = new Vector<String>();

    private TGLevelObject tgLevelObject = new TGLevelObject(0,"说明",0);

    public TGPanel(){

        gameLevelName.add("说明");
        gameLevelName.add("指法练习");
        gameLevelName.add("小写字母顺序练习");
        gameLevelName.add("数字顺序练习");
        gameLevelName.add("小写字母随机练习");
        gameLevelName.add("数字随机练习");
        gameLevelName.add("小写字母、大写字母和数字组合练习");
        gameLevelName.add("小写字母，速度5");
        //gameLevelName.add("小写字母，速度7");
        gameLevelName.add("汉字词组");
        gameLevelName.add("汉字");
        startRun = false;
        this.requestFocus();
    }

    public void dealZhChar(String data){
        this.tgLevelObject.dealZhChar(data);
    }

    public void actionPerformed(ActionEvent e){
        this.requestFocus();
    }

    public String getGameLevelName(int gameLevel){
        if (gameLevel < 0 || gameLevel >= gameLevelName.size()){
            return "无限挑战通关模式";
        }else {
            return gameLevelName.get(gameLevel);
        }
    }

    public void drawGameInfo(Graphics g){
        if (gameLevel == 0 || startRun == false){
            g.setColor(Color.black);
            g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
            for (int i = 0; i < gameLevelName.size(); i++){
                int y = 25*(i+1)+100;
                g.drawString("[第"+i+"关]"+getGameLevelName(i), 10, y);
            }
        }
        g.setColor(Color.green);
        g.setFont(new Font("TimesRoman", Font.PLAIN, 15));
        g.drawString("按[F1]键重新开始", 10, TypeGameApp.getFrameHeight() - 100);
        g.drawString("按[F2]键开始下一关", 150, TypeGameApp.getFrameHeight() - 100);
        g.drawString("按[F3]键结束游戏", 300, TypeGameApp.getFrameHeight() - 100);
        g.drawString("按[数字]键选择关卡", 450, TypeGameApp.getFrameHeight() - 100);
        g.drawString("按[ESC]键退出游戏", 600, TypeGameApp.getFrameHeight() - 100);
    }

    @Override
    public void paint(Graphics g){
        boolean isContinue = tgLevelObject.draw(g);
        if (isContinue == false){
            startRun = false;
        }
        drawGameInfo(g);
    }
    int width = TypeGameApp.frame.getWidth();
    int height = TypeGameApp.frame.getHeight();
    Image image = null;
    //双缓冲
    @Override
    public void update(Graphics g){

        if (width != TypeGameApp.frame.getWidth() || height != TypeGameApp.frame.getHeight()){
            width = TypeGameApp.frame.getWidth();
            height = TypeGameApp.frame.getHeight();
            image = this.createImage(width, height);
        }

        if (image == null){
            image = this.createImage(width,height);
        }

        Graphics graphics = image.getGraphics();
        Color col = graphics.getColor();
        graphics.setColor(Color.black);
        graphics.fillRect(0,0,width,height);
        paint(graphics);
        g.drawImage(image, 0, 0, null);
    }

    public void restart(){
        tgLevelObject.restart();
        startRun = true;
    }
    public void stop(){
        tgLevelObject.stop();
        startRun = false;
    }

    public void keyPressed(KeyEvent e){
        //logger.info("keyPressed="+e.getKeyChar());
        char typeC = e.getKeyChar();
        int keyCode = e.getKeyCode();

        if(true == startRun) {
            tgLevelObject.dealPressed(e);
        }

        //F1 重新开始游戏
        if (keyCode == 112){
            if (gameLevel != 0) {
                restart();
            }
        }
        //F3结束游戏
        if (keyCode == 114){
            stop();
        }
        //F2 下一关
        if ((startRun == false&&keyCode == 113)
                ||(startRun == false && typeC >= '1' && typeC <= '9')){
            if (keyCode == 113){
                gameLevel++;
            }else{
                //停止情况下按数字选关卡
                gameLevel = typeC-'0';
            }

            TypeGameApp.textField.setVisible(false);
            TypeGameApp.textField.repaint();
            this.requestFocus();
            tgLevelObject.clear();
            tgLevelObject.setDataOnceNumber(1);//默认一次一个
            switch (gameLevel){
                case 1:
                    level1();
                    break;
                case 2:
                    level2();
                    break;
                case 3:
                    level3();
                    break;
                case 4:
                    level4();
                    break;
                case 5:
                    level5();
                    break;
                case 6:
                    level6();
                    break;
                case 7:
                    levelSpeed(5);
                    break;
                case 8:
                    //levelSpeed(7);
                    levelZhCharCiZu();
                    TypeGameApp.textField.setVisible(true);
                    TypeGameApp.textField.repaint();
                    TypeGameApp.textField.requestFocus();
                    break;
                case 9:
                    levelZhChar();
                    TypeGameApp.textField.setVisible(true);
                    TypeGameApp.textField.repaint();
                    TypeGameApp.textField.requestFocus();
                    break;
                    default:
                        tgLevelObject.clearNextCList();
            }

            if (gameLevel < gameLevelName.size()) {
                tgLevelObject.setLevel(gameLevel, getGameLevelName(gameLevel),1,100,20);
            }else{
                tgLevelObject.setLevel(gameLevel,getGameLevelName(gameLevel), gameLevel - gameLevelName.size()+1,100,20);
            }
            tgLevelObject.setIsRun(true);
            tgLevelObject.init();
            startRun = true;
        }
        //esc退出游戏
        if (typeC == 27){
            System.exit(0);
        }

    }

    public void keyTyped(KeyEvent e){
    }

    public void keyReleased(KeyEvent e){
    }

    public void run(){
        while (true){
            try{
                if (startRun == false){
                    Thread.sleep(1000);
                    continue;
                }
                tgLevelObject.runDown();
                Thread.sleep(50);
            }catch (Exception e){
                e.printStackTrace();
            }
            repaint();
        }
    }

    //指法练习
    void level1(){
        Vector<Character> characterVector = new Vector<Character>();
        characterVector.add('a');
        characterVector.add('s');
        characterVector.add('d');
        characterVector.add('f');
        characterVector.add('g');
        characterVector.add('h');
        characterVector.add('j');
        characterVector.add('k');
        characterVector.add('l');

        characterVector.add('q');
        characterVector.add('w');
        characterVector.add('e');
        characterVector.add('r');
        characterVector.add('t');
        characterVector.add('y');
        characterVector.add('u');
        characterVector.add('i');
        characterVector.add('o');
        characterVector.add('p');

        characterVector.add('z');
        characterVector.add('x');
        characterVector.add('c');
        characterVector.add('v');
        characterVector.add('b');
        characterVector.add('n');
        characterVector.add('m');
        tgLevelObject.setNextCList(characterVector);
    }

    //小写字母顺序练习
    void level2(){
        Vector<Character> characterVector = new Vector<Character>();
        char baseC = 'a';
        for (int i = 0; i < 26; i++) {
            characterVector.add((char)(baseC+i));
        }
        tgLevelObject.setNextCList(characterVector);
    }

    //数字顺序练习
    void level3(){
        Vector<Character> characterVector = new Vector<Character>();
        char baseC = '0';
        for (int i = 0; i < 10; i++) {
            characterVector.add((char)(baseC+i));
        }

        tgLevelObject.setNextCList(characterVector);
    }

    //小写字母随机练习
    void level4(){
        tgLevelObject.clearNextCList();
        tgLevelObject.setRandomCharIndex(1);
    }

    //数字随机练习
    void level5(){
        tgLevelObject.clearNextCList();
        tgLevelObject.setRandomCharIndex(3);
    }

    //小写字母、大写字母和数字组合练习
    void level6(){
        tgLevelObject.clearNextCList();
        tgLevelObject.setRandomCharIndex(-1);
    }
    //小写字母，速度
    void levelSpeed(int speed){
        tgLevelObject.setStepYIndex(speed);
        tgLevelObject.clearNextCList();
        tgLevelObject.setRandomCharIndex(1);
    }
    //汉字词组
    void levelZhCharCiZu(){
        tgLevelObject.setCharLevel(false);
        tgLevelObject.setStepYIndex(2);
        tgLevelObject.setRandomDataIndex(2);
    }
    //汉字
    void levelZhChar(){
        tgLevelObject.setCharLevel(false);
        tgLevelObject.setStepYIndex(2);
    }
}
